package com.zhangchuchu.neohealth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhangchuchu.neohealth.entity.DTO.FavoritesDTO;
import com.zhangchuchu.neohealth.entity.Favorites;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FavoritesMapper extends BaseMapper<Favorites> {
    // 查询用户收藏的文章（按收藏时间倒序）
    @Select("""
        SELECT a.*, f.url, f.user_id
        FROM favorites f
        JOIN articles a ON f.target_id = a.id
        WHERE f.user_id = #{userId}
          AND f.target_type = 'article'
        ORDER BY f.create_time DESC
    """)
    List<FavoritesDTO> getFavoriteArticles(@Param("userId") Integer userId);

    // 查询用户收藏的贴文（按收藏时间倒序）
    @Select("""
        SELECT c.*, f.url, f.user_id
        FROM favorites f
        JOIN community c ON f.target_id = c.id
        WHERE f.user_id = #{userId}
          AND f.target_type = 'post'
        ORDER BY f.create_time DESC
    """)
    List<FavoritesDTO> getFavoritePosts(@Param("userId") Integer userId);

}
